	/**
 * 
 */
package ar.edu.utn.frgp.banco.servlets.registro;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ar.edu.utn.frgp.banco.domain.Cliente;
import ar.edu.utn.frgp.banco.exceptions.PersonalisedException;
import ar.edu.utn.frgp.banco.service.OperacionesClienteServ;
import ar.edu.utn.frgp.banco.service.impl.OperacionesClienteServImpl;
import ar.edu.utn.frgp.banco.servlets.SuperServlet;

/**
 * @author Backup_LABS
 *
 */
public class Registro extends SuperServlet {

	Cliente cliente; 
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 9183042699912561958L;
	
	
	private static OperacionesClienteServ gestion;
	
	public Registro(){
		if(gestion == null) gestion = new OperacionesClienteServImpl();
	}
	
	/* (non-Javadoc)
	 * @see ar.edu.utn.frgp.banco.servlets.SuperServlet#execute(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	public String execute(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException, Exception {
				
		req.setAttribute("clienteNuevo", cliente);
		try{
			gestion.crearCliente(cliente);
			return "/exc/registro/registroOK.jsp";
		} catch (PersonalisedException e) {
			addError(e.getError());
		}
		return "/exc/registro/registro.jsp";
	}
	
	public String cancelar(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException, Exception {
		
		return "/exc/login.jsp";
	}

	@Override
	public boolean populate(HttpServletRequest req) {
		cliente = new Cliente();
		
		cliente.setDni(req.getParameter("dni"));
		cliente.setApellido(req.getParameter("apellido"));
		cliente.setClave(req.getParameter("clave"));
		cliente.setNombre(req.getParameter("nombre"));
		cliente.setDireccion(req.getParameter("direccion"));
		cliente.setMail(req.getParameter("mail"));
		return true;
	}

}
